package com.alinesno.cloud.base.logger.consumer;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alinesno.cloud.base.logger.entity.LogSystemFilesEntity;
import com.alinesno.cloud.base.logger.entity.LogSystemStatusEntity;
import com.alinesno.cloud.base.logger.service.ILogSystemFilesService;
import com.alinesno.cloud.base.logger.service.ILogSystemStatusService;
import com.alinesno.cloud.monitor.agent.aspect.BaseAspect;

/**
 * 异常消息监听
 * 
 * @author WeiXiaoJin
 * @since 2020年1月25日 下午19:45:08
 */
@Component
class MontorSystemConsumerListener extends BaseConsumer {
	
	private static final Logger log = LoggerFactory.getLogger(MontorSystemConsumerListener.class) ; 

	@Autowired
	private ILogSystemStatusService logSystemStatusService ;
	
	@Autowired
	private ILogSystemFilesService logSystemFilesService ; 

	/**
	 * 服务状态监听 
	 * @param record
	 * @param data
	 * @param partition
	 * @param topic
	 * @param ts
	 * @throws Exception 
	 */
	@KafkaListener(topics = { BaseAspect.WATCHER_TOPIC_SYSTEM_MONITOR})
	public void listenLogger(ConsumerRecord<?, ?> record, @Payload String data,
			@Header(KafkaHeaders.RECEIVED_PARTITION_ID) int partition,
			@Header(KafkaHeaders.RECEIVED_TOPIC) String topic, @Header(KafkaHeaders.RECEIVED_TIMESTAMP) long ts) throws Exception {

		Optional<?> kafkaMessage = Optional.ofNullable(record.value());
		if (kafkaMessage.isPresent()) {
			
			Object message = kafkaMessage.get();
			
			log.debug("---------------->>>>  message = topic：" + topic + ", " + message);
			
			if(message != null) {
				buildStorageMessage(message+""); ; 
			}
		}
	}
	
	public void buildStorageMessage(String text) {
		
		LogSystemStatusEntity s = new LogSystemStatusEntity() ; 

		// HOSTNAME
		JSONObject json = JSONObject.parseObject(text) ;
		String applicationName = json.getString(APPLICATIONNAME) ;
		String hostName = json.getString(HOSTNAME) ;
		String ip = json.getString(IP) ; 
		
		s.setApplicationName(applicationName);
		s.setHostname(hostName);
		s.setIp(ip); 
	
		// CPU
		JSONObject cpuJson = json.getJSONObject("cpu") ; 
		int cpuNum = cpuJson.getIntValue("cpuNum") ;
		double cpuFree = cpuJson.getDoubleValue("free") ; 
		double cpuSys = cpuJson.getDoubleValue("sys") ; 
		double cpuTotal = cpuJson.getDoubleValue("total") ; 
		double cpuUsed = cpuJson.getDoubleValue("used") ; 
		double cpuWait = cpuJson.getDoubleValue("wait") ; 
		
		s.setCpuNum(cpuNum);
		s.setCpuFree(cpuFree);
		s.setCpuSys(cpuSys);
		s.setCpuTotal(cpuTotal);
		s.setCpuUsed(cpuUsed);
		s.setCpuWait(cpuWait);
	
		// JVM
		JSONObject jvmJson = json.getJSONObject("jvm") ; 
		double jvmFree = jvmJson.getDoubleValue("free") ; 
		String jvmHome = jvmJson.getString("home") ; 
		double jvmMax = jvmJson.getDoubleValue("max") ; 
		String jvmName = jvmJson.getString("name") ; 
		String jvmRunTime = jvmJson.getString("runTime") ; 
		String jvmStartTime = jvmJson.getString("startTime") ; 
		double jvmTotal = jvmJson.getDoubleValue("total") ; 
		double jvmUsage = jvmJson.getDoubleValue("usage") ; 
		String jvmVersion = jvmJson.getString("version") ; 
		
		s.setJvmFree(jvmFree);
		s.setJvmHome(jvmHome);
		s.setJvmMax(jvmMax);
		s.setJvmTotal(jvmTotal);
		s.setJvmVersion(jvmVersion);
		s.setJvmName(jvmName);
		s.setJvmRunTime(jvmRunTime);
		s.setJvmStartTime(jvmStartTime);
		s.setJvmUsage(jvmUsage);
	
		// MEM
		JSONObject memJson = json.getJSONObject("mem") ; 
		double memFree = memJson.getDoubleValue("free") ; 
		double memTotal = memJson.getDoubleValue("total") ; 
		double memUsage = memJson.getDoubleValue("usage") ; 
		double memUsed = memJson.getDoubleValue("used") ; 
	
		s.setMemFree(memFree); 
		s.setMemTotal(memTotal);
		s.setMemUsage(memUsage);
		s.setMemUsed(memUsed);
		
		// SYS
		JSONObject sysJson = json.getJSONObject("sys") ; 
		String sysComputerIP = sysJson.getString("computerIp") ; 
		String sysComputerName = sysJson.getString("computerName") ; 
		String sysOsArch = sysJson.getString("osArch") ; 
		String sysOsName = sysJson.getString("osName") ; 
		String sysUserDir = sysJson.getString("userDir") ; 
		
		s.setServerOsName(sysOsName);
		s.setServerUserDir(sysUserDir);
		s.setServerIp(sysComputerIP);
		s.setServerName(sysComputerName);
		s.setServerOsArch(sysOsArch);
		
		// >>>>>>>---------------------------------------------------------->>>>>>>>
		s.setAddTime(new Date());
		logSystemStatusService.save(s) ; 
	
		// SYS_FILES
		JSONArray sysFilesJson = json.getJSONArray("sysFiles") ; 
		List<LogSystemFilesEntity> files = new ArrayList<LogSystemFilesEntity>() ; // JSONArray.parseArray(sysFilesJson.toJSONString(), LogSystemFilesEntity.class);
		
		for(Object o : sysFilesJson) {
			JSONObject obj = JSONObject.parseObject(o+"") ;
			LogSystemFilesEntity e = new LogSystemFilesEntity() ; 
			
			String dirName = obj.getString("dirName") ; 
			String free = obj.getString("free") ; 
			String sysTypeName = obj.getString("sysTypeName") ; 
			String total = obj.getString("total") ; 
			String typeName = obj.getString("typeName") ; 
			double usage = obj.getDoubleValue("usage") ; 
			String used = obj.getString("used") ; 
			
			e.setDirName(dirName);
			e.setDirFree(free);
			e.setSysTypeName(sysTypeName);
			e.setTotal(total);
			e.setFilesUsage(usage);
			e.setTypeName(typeName);
			e.setFilesUsed(used);
			e.setSystemStatusId(s.getId());
			
			e.setAddTime(new Date());
			
			files.add(e) ; 
		}
		
		logSystemFilesService.saveAll(files) ; 
	}
}










































